package com.jm.web.form.baseitem;

import java.io.IOException;
import java.sql.ResultSet;

import javax.servlet.jsp.JspWriter;

import com.jm.utils.AttributeBuilder;
import com.jm.utils.StringBuilderEx;
import com.jm.utils.StringHelper;
import com.jm.web.form.support.FieldItem;
import com.jm.web.form.support.FormItem;
import com.jm.web.tags.support.BaseTag;

public class YarncontItem extends ListItem{
	protected StringBuffer data = new StringBuffer();
	protected boolean bDataEmpty = true;
	protected String otherdata = "";
	
	public YarncontItem() {};
	
	@Override
	public void InitTag(BaseTag tag) {
		super.InitTag(tag);
		this.setNodeName(FieldItem.TAG_ITEMNAME);
	}
	
	@Override
	protected void processDataSourcelist() {
		if(this.getUtils() != null ){
			try {
				String key = StringHelper.Format("combox-%1$s-%2$s",this.strDataSource,this.strSql);
				if(this.getBaseTag().getPage().listItemDataContainsKey(key)){
					otherdata = (String)this.getBaseTag().getPage().getListItemData(key);
				}else{
					ResultSet rs = this.getUtils().executeQuery(StringHelper.getValue(this.strDataSource,"info"), StringHelper.getValue(this.strSql, "select enElemFullName  from sys..sys007 where enab > 0 and enElemFullName <> '' order by enElemFullName"));					
					boolean bfirst = true;
					while(rs.next())
					{
						if(!bfirst){
							data.append(",");
						}
						data.append("\"").append(StringHelper.getValue(rs.getString(1))).append("\":true");
						bfirst = false;
					}
					rs.close();
					this.getBaseTag().getPage().addListItemData(key,this.getName());
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	@Override
	public void renderBegin(JspWriter writer) {
		if(this.getBaseTag() != null && this.getBaseTag().isDSP())
			super.renderBegin(writer);
	}
	
	@Override
	public void renderEnd(JspWriter writer) {
		if(this.getBaseTag() != null && this.getBaseTag().isDSP())
			super.renderEnd(writer);
	}
	
	@Override
	public void onRenderSingleBody(JspWriter writer, FormItem item) {
		StringBuilderEx sb = new StringBuilderEx();
		int textWidth = Integer.parseInt(StringHelper.getValue(this.getWidth(),"100").replace("px",""));
		
		if(this.isTemplate()){
			sb.Append("<div id=\"yarncont_tp_div\" >");
		}
		
		this.setWidth(StringHelper.Format("%1$spx",textWidth));
		this.setStyle(StringHelper.Format("%1$s;width:%2$spx;",this.getStyle(),textWidth));
		
		//1.combox 
		sb.Append("\r\n<input type='text' xtype=\"combobox\" ");
		sb.Append(" onblur=\"$P.form.comboboxblur(this)\" ");		
		AttributeBuilder attributesBuilder = new AttributeBuilder();
		attributesBuilder.InitFromHashtable(this.extAttrList,true);
		FillAttributeBuilder(attributesBuilder);
		
		attributesBuilder.Remove("required");
		attributesBuilder.Remove("rule");
		attributesBuilder.Remove("msg");
		
		
		if(!StringHelper.IsNullOrEmpty(this.getValue()))
		{
			attributesBuilder.Set(TAG_VALUE.toLowerCase(), this.getValue());
		}
		
		sb.Append(attributesBuilder.ToOutputString());
		if(this.isTemplate()){
			sb.Append(" />");
		}
		
		sb.Append("</div>");
		try {
			writer.write(sb.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	

	public StringBuffer getData() {
		return data;
	}


	public void setData(StringBuffer data) {
		this.data = data;
	}
	public String getOtherdata() {
		return otherdata;
	}
	
	public void setOtherdata(String otherdata) {
		this.otherdata = otherdata;
	}
	
	public boolean isTemplate(){
		return this.GetExtValue("template", false);
	}
}
